docx文档转pdf文件

您所在的位置:网站首页 docx转pdf java docx文档转pdf文件

docx文档转pdf文件

2023-07-20 23:20| 来源: 网络整理| 查看: 265

注:这里只能转换docx文件,doc不行,源码在文末。

可以在windows,linux上运行,增加了内容替换功能,因为有些文档内容需要我们用代码来动态生成。

下面是具体操作步骤:

maven依赖 org.apache.poi poi-scratchpad 3.11 org.apache.poi ooxml-schemas 1.1 com.itextpdf itextpdf 5.4.3 fr.opensagres.xdocreport org.apache.poi.xwpf.converter.pdf 1.0.6 fr.opensagres.xdocreport org.apache.poi.xwpf.converter.xhtml 1.0.6 org.docx4j docx4j-ImportXHTML 3.2.0 java代码 package com.gitee.docx2pdf; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.poi.xwpf.converter.pdf.PdfConverter; import org.apache.poi.xwpf.converter.pdf.PdfOptions; import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter; import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import com.lowagie.text.Font; import com.lowagie.text.pdf.BaseFont; import fr.opensagres.xdocreport.itext.extension.font.IFontProvider; /** * 文档工具类 */ public class DocUtil { public static void main(String[] args) throws Exception { String docx = "1.docx"; String pdf = "1.pdf"; // 直接转换 InputStream docxStream = DocUtil.class.getClassLoader().getResourceAsStream(docx); byte[] pdfData = docxToPdf(docxStream); FileUtils.writeByteArrayToFile(new File(pdf), pdfData); // 替换内容后转换例子 InputStream docxStream2 = DocUtil.class.getClassLoader().getResourceAsStream("2.docx"); Map data = new HashMap(); data.put("{title}", "标题内容"); data.put("{username}", "张三"); byte[] pdfData2 = bindDocxDataAndToPdf(docxStream2, data); FileUtils.writeByteArrayToFile(new File("data.pdf"), pdfData2); System.out.println("finished."); } /** * 替换docx文件内容,并转换成PDF * * @param input * @param data * @return * @throws Exception */ public static byte[] bindDocxDataAndToPdf(InputStream input, Map data) throws Exception { byte[] replacedContent = replaceDocxContent(input, data); byte[] pdfData = docxToPdf(new ByteArrayInputStream(replacedContent)); return pdfData; } /** * docx转成pdf * * @param docxStream * docx文件流 * @return 返回pdf数据 * @throws Exception */ public static byte[] docxToPdf(InputStream docxStream) throws Exception { ByteArrayOutputStream targetStream = null; XWPFDocument doc = null; try { doc = new XWPFDocument(docxStream); PdfOptions options = PdfOptions.create(); // 中文字体处理 options.fontProvider(new IFontProvider() { @Override public Font getFont(String familyName, String encoding, float size, int style, java.awt.Color color) { try { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(bfChinese, 12, style, color); if (familyName != null) fontChinese.setFamily(familyName); return fontChinese; } catch (Exception e) { e.printStackTrace(); return null; } } }); targetStream = new ByteArrayOutputStream(); PdfConverter.getInstance().convert(doc, targetStream, options); return targetStream.toByteArray(); } catch (IOException e) { throw new Exception(e); } finally { IOUtils.closeQuietly(targetStream); } } /** * docx转换成html内容 * * @param docxIn * docx文件输入流 * @return * @throws Exception */ public static byte[] docxToHtml(InputStream docxIn) throws Exception { ByteArrayOutputStream out = null; try { XWPFDocument document = new XWPFDocument(docxIn); XHTMLOptions options = XHTMLOptions.create(); out = new ByteArrayOutputStream(); XHTMLConverter.getInstance().convert(document, out, options); return out.toByteArray(); } catch (IOException e) { throw new Exception(e); } finally { IOUtils.closeQuietly(out); } } /** * 替换docx内容 * * @param in * docx输入流 * @param map * 替换键值对 * @return 返回替换后的文件流 * @throws Exception */ public static byte[] replaceDocxContent(InputStream in, Map map) throws Exception { // 读取word模板 XWPFDocument hdt = null; ByteArrayOutputStream out = null; try { hdt = new XWPFDocument(in); // 替换段落内容 List paragraphs = hdt.getParagraphs(); replaceParagraphsContent(paragraphs, map); // 替换表格内容 List tables = hdt.getTables(); // 读取表格 for (XWPFTable table : tables) { int rcount = table.getNumberOfRows(); // 遍历表格中的行 for (int i = 0; i < rcount; i++) { XWPFTableRow row = table.getRow(i); // 遍历行中的单元格 List cells = row.getTableCells(); for (XWPFTableCell cell : cells) { List cellParagraphs = cell.getParagraphs(); replaceParagraphsContent(cellParagraphs, map); } } } out = new ByteArrayOutputStream(); hdt.write(out); return out.toByteArray(); } catch (IOException e) { throw new Exception(e.getMessage()); } finally { IOUtils.closeQuietly(out); } } private static void replaceParagraphsContent(List paragraphs, Map map) { for (XWPFParagraph paragraph : paragraphs) { List runs = paragraph.getRuns(); for (XWPFRun run : runs) { String text = run.getText(0); if (text != null) { boolean isSetText = false; for (Entry entry : map.entrySet()) { String key = entry.getKey(); if (text.indexOf(key) != -1) {// 在配置文件中有这个关键字对应的键 String value = entry.getValue(); if (value == null) { throw new RuntimeException(key + "对应的值不能为null"); } // 文本替换 text = text.replace(key, value); isSetText = true; } } if (isSetText) { run.setText(text, 0); } } } } } } 添加字体资源文件

把字体资源文件添加到resources下面,具体内容在下面的源码中可查看。 这里的资源文件是直接从itextasian-1.5.2.jar下拷贝过来的,因为itextasian-1.5.2.jar在中央仓库已经下载不到了。

最后运行DocUtil 中的main方法进行测试。

源代码:点击前往



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3